<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/IEntity.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/utils/System.php'; ?>


<?php

abstract class GenericDAO {

    var $conexao;

    public function __construct() {
        $this->conexao = System::getConexao();
    }

    /**
     *
     * @param IEntity $entity
     * @return type 
     */
    function findAll(IEntity $entity) {
        $list = array();
        $registro = 0;

        $sql = "SELECT * FROM " . $entity;
        
        $result = mysql_query($sql);

        if ($result) {
            while ($row = mysql_fetch_assoc($result)) {
                $list[$registro] = $row;
                $registro++;
            }
        }

        //System::desconectar();

        return $list;
    }

    /**
     *
     * @param IEntity $entity
     * @param type $valores
     * @return boolean 
     */
    function insert(IEntity $entity, $valores) {

        $campos = $entity->getAttributes();

        $count = 0;

        foreach ($valores as $valor) {
            if (!is_numeric($valor)) {
                $valor = "'" . $valor . "'";
                $valores[$count] = $valor;
            }
            $count++;
        }
        
        
        $sql = "INSERT INTO " . $entity . " (" . implode(', ', $campos) . ") " . " 
            VALUES (" . implode(', ', $valores) . ")";

        $result = mysql_query($sql);
        
        echo $sql;
        
        if ($result) {
            return true;
        }

        return false;
    }

    function delete(IEntity $entity, $id) {
        $sql = "DELETE FROM " . $entity . " WHERE id =" . $id;

        $result = mysql_query($sql);

        if ($result) {
//            System::desconectar();
            return true;
        }

//        System::desconectar();
        return false;
    }

}
?>
